
/*============================================================================*/
/** Copyright (C) 2009-2011, iSOFT INFRASTRUCTURE SOFTWARE CO.,LTD.
 *
 *  All rights reserved. This software is iSOFT property. Duplication
 *  or disclosure without iSOFT written authorization is prohibited.
 *
 *  @file       <Init.c>
 *  @brief      <>
 *
 *  <Detailed description of the file(multi-line)>
 *
 *  @author
 *  @date
 */
/*============================================================================*/

/*=======[M I S R A C  R U L E  V I O L A T I O N]============================*/
/*  <MESSAGE ID>    <CODE LINE>    <REASON>
 */
/*============================================================================*/

/*=======[I N C L U D E S]====================================================*/
#include "Os.h"
#include "OsRegs.h"
#include "Mcu.h"

/*=======[M A C R O S]========================================================*/
#define ArchDisableInt() __disable()
#define ArchEnableInt()  __enable()
/*=======[T Y P E   D E F I N I T I O N S]====================================*/

/*=======[E X T E R N A L   D A T A]==========================================*/

/*=======[E X T E R N A L   F U N C T I O N   D E C L A R A T I O N S]========*/

/*=======[I N T E R N A L   D A T A]==========================================*/
volatile static TickType counterCurVal = 0;
/*=======[I N T E R N A L   F U N C T I O N   D E C L A R A T I O N S]========*/

/*=======[F U N C T I O N   I M P L E M E N T A T I O N S]====================*/
/******************************************************************************/
/*
 * Brief                <MAIN_vWriteWDTCON0>
 *
 * Service ID   :       <None>
 * Sync/Async   :       <Synchronous>
 * Reentrancy           <Non Reentrant>
 * @param[in]           <None>
 * @param[out]          <None>
 * @param[in/out]       <None>
 * @return              <None>
 * PreCondition         <None>
 * CallByAPI            <NULL>
 */
/******************************************************************************/
void MAIN_vWriteWDTCON0(uint32 uwValue)
{
    uint32 uwDummy;
    uwDummy = OS_WDT_CON0;
    uwDummy |= 0x000000F0;                         /* set HWPW1 = 1111b*/
    if (OS_WDT_CON1_DR != 0)
    {
        uwDummy |= 0x00000008;                     /* set HWPW0 = WDTDR*/
    }
    else
    {
        uwDummy &= ~0x00000008;                    /* set HWPW0 = WDTDR*/
    }
    if (OS_WDT_CON1_IR != 0)
    {
        uwDummy |= 0x00000004;                     /* set HWPW0 = WDTIR*/
    }
    else
    {
        uwDummy &= ~0x00000004;                     /* set HWPW0 = WDTIR*/
    }
    uwDummy &= ~0x00000002;                         /* set WDTLCK = 0*/
    OS_WDT_CON0 = uwDummy;                             /* unlock access*/
    uwValue |=  0x000000F2;                         /* set HWPW1 = 1111b and WDTLCK = 1*/
    uwValue &= ~0x0000000C;                         /* set HWPW0 = 00b*/
    OS_WDT_CON0 = uwValue;                             /* write access and lock*/
}

/******************************************************************************/
/*
 * Brief                <init the STM0 interrupt>
 *
 * Service ID   :       <None>
 * Sync/Async   :       <Synchronous>
 * Reentrancy           <Non Reentrant>
 * @param[in]           <None>
 * @param[out]          <None>
 * @param[in/out]       <None>
 * @return              <None>
 * PreCondition         <None>
 * CallByAPI            <NULL>
 */
/******************************************************************************/
 static void Os_InitSTM0(void)
{
    MAIN_vResetENDINIT();
    OS_STM_CLC = 0x00000200;               /* load clock control register, 0.1us*/
    MAIN_vSetENDINIT();
    OS_STM_CMP0 = DECR_V/2;                /* load compare register 0, 10000*0.1us=1ms*/
    OS_STM_CMCON = 0x00170017;             /* load compare match control register*/
    OS_STM_ICR = 0x00000051;               /* load interrupt control register*/
	OS_STM_SRC0 = (OS_STM_SRC0 &0xffffff00)|0x00001000 + 255;
}

/******************************************************************************/
 /*
  * Brief                <init the PLL>
  *
  * Service ID   :       <None>
  * Sync/Async   :       <Synchronous>
  * Reentrancy           <Non Reentrant>
  * @param[in]           <None>
  * @param[out]          <None>
  * @param[in/out]       <None>
  * @return              <None>
  * PreCondition         <None>
  * CallByAPI            <NULL>
  */
/******************************************************************************/
static void Os_InitPLL(void)
{
    Mcu_Init(NULL);
    Mcu_InitClock(0);
}

/******************************************************************************/
 /*
  * Brief                <init the CPU>
  *
  * Service ID   :       <None>
  * Sync/Async   :       <Synchronous>
  * Reentrancy           <Non Reentrant>
  * @param[in]           <None>
  * @param[out]          <None>
  * @param[in/out]       <None>
  * @return              <None>
  * PreCondition         <None>
  * CallByAPI            <NULL>
  */
/******************************************************************************/
void OSInitCPU(void)
{
	Os_InitPLL();
    Os_InitSTM0();
}


/******************************************************************************/
/*
 * Brief                <init os and start os>
 *
 * Service ID   :       <OSServiceId_StartOS>
 * Sync/Async   :       <Synchronous>
 * Reentrancy           <Non Reentrant>
 * @param[in]           <None>
 * @param[out]          <None>
 * @param[in/out]       <None>
 * @return              <None>
 * PreCondition         <None>
 * CallByAPI            <main>
 */
/******************************************************************************/
void InitSystem(void)
{
	ArchDisableInt();
	OSInitCPU();
}

/******************************************************************************/
/*
 * Brief                <STM_viSRN0>
 *
 * Service ID   :       <None>
 * Sync/Async   :       <Synchronous>
 * Reentrancy           <Non Reentrant>
 * @param[in]           <None>
 * @param[out]          <None>
 * @param[in/out]       <None>
 * @return              <None>
 * PreCondition         <None>
 * CallByAPI            <NULL>
 */
/******************************************************************************/
void STM_viSRN0(void)
{
	if( OS_STM_ICR_CMP0IR == 1 )
	{
		OS_STM_CMP0 = DECR_V/2+OS_STM_CMP0;
		OS_STM_ISRR_CMP0IRR = 1;
	}
	counterCurVal++;
}


/******************************************************************************/
/*
 * Brief                <Get current counter value>
 * Service ID           <None>
 * Sync/Async           <Synchronous>
 * Reentrancy           <Reentrant>
 * param-CounterID[in]  <reference to counter>
 * Param-Value[out]     <current value of the counter>
 * Param-Name[in/out]   <None>
 * return               <None>
 * PreCondition         <None>
 * CallByAPI            <None>
 */
/******************************************************************************/
Std_ReturnType GetCounterValue(CounterType CounterID,TickRefType Value)
{
	CounterID = CounterID;
	*Value = counterCurVal;
	return E_OK;
}

/******************************************************************************/
/*
 * Brief                    <Get elapsed counter value>
 * Service ID               <None>
 * Sync/Async               <Synchronous>
 * Reentrancy               <Reentrant>
 * param-CounterID[in]      <reference to counter>
 * Param-Value[out]         <current value of the counter>
 * Param-ElapsedValue[out]  <Elapsed value of the counter>
 * Param-Name[in/out]       <None>
 * return                   <StatusType>
 * PreCondition             <None>
 * CallByAPI                <None>
 */
/******************************************************************************/
Std_ReturnType GetElapsedCounterValue(CounterType CounterID,TickRefType Value,TickRefType ElapsedValue)
{
	CounterID = CounterID;

	*ElapsedValue = (counterCurVal - *Value);
	*Value = counterCurVal;
	return E_OK;
}


/******************************************************************************/
/*
 * Brief                <INT_SRN_255>
 *
 * Service ID   :       <None>
 * Sync/Async   :       <Synchronous>
 * Reentrancy           <Non Reentrant>
 * @param[in]           <None>
 * @param[out]          <None>
 * @param[in/out]       <None>
 * @return              <None>
 * PreCondition         <None>
 * CallByAPI            <NULL>
 */
/******************************************************************************/
void __interrupt (255)  INT_SRN_255(void)
{
	STM_viSRN0();
}

/*=======[E N D   O F   F I L E]==============================================*/
